<%@include file="WEB-INF/jspf/header.jspf" %>
<% 
if (user.getRole()!=1) { 
// if the user does not exist in the session, redirect to login page
%>
    <jsp:forward page="/error.jsp" />
<% }
// this just avoids null pointer exceptions
if (action == null) {
    action = "";    
}
%>
<h2>User Management</h2>

<!-- Add a user -->
<% 
    if (action.equalsIgnoreCase("add_user")) {
%>
<form action="administrator.do" name="createuser" method="POST" enctype="text/html">
    <table border="0" cellpadding="2">
        <tr>
            <td>First Name: </td>
            <td><input type="text" name="firstname"  style="width: 300px; " /></td>
        </tr>
        <tr>
            <td>Last Name: </td>
            <td><input type="text" name="lastname"  style="width: 300px; " /></td>
        </tr>
        <tr>
            <td>Email: </td>
            <td><input type="text" name="email" style="width: 300px; " /></td>
        </tr>
        <tr>
            <td>Username: </td>
            <td><input type="text" name="user_name" value="" style="width: 300px; "  /></td>
        </tr>
        <tr>
            <td>Password: </td>
            <td><input type="password" name="password" value="" style="width: 300px; " /></td>
        </tr>  
        <tr>
            <td>Confirm Password: </td>
            <td><input type="password" name="confirmpassword"  style="width: 300px; " /></td>
        </tr>
        <tr>
            <td>Role:</td>
            <td><select name="role">
                    <option value="1">1 Admin</option>
                    <option value="2">2 Writer</option>
                    <option value="3">3 Reader</option>
                </select>
            </td>
        <tr>
            <td>&nbsp;</td>
            <td>
                <input type="hidden" name="page" value="adminview.jsp" />
                <input type="submit" name="action" value="Add User"  />
                <input type="submit" name="action" value="Cancel"  />  
            </td>
        </tr>
    </table>
</form>
<% } else if (session.getAttribute("user_updated_success") != null) {
    String success = (String)session.getAttribute("user_updated_success");
%>
<p class="success">Successfully updated <%= success %></p>

<% } else if (session.getAttribute("user_added_success") != null ) {
    String success = (String)session.getAttribute("user_added_success");
    session.removeAttribute("user_added_success");
%>
<p class="success">Successfully added <%= success %></p>
<% }  else if (request.getAttribute("error_null_fields") != null) { %>
<p class="error">You must fill in all fields when creating a user, please try again</p>
<% }  else if (request.getAttribute("error_passwords_no_match") != null) { %>
<p class="error">The passwords did not match, please try again</p>
<% } %>

<p>Actions: <a href="adminview.jsp?action=add_user">Add New User</a></p>


<h3>Users in System</h3>
<p>To update a user, click on their name</p>
<ul>
<% 
    Collection userList = (Collection)session.getAttribute("userlist");
    java.util.Collections.sort((List)userList);
    for (Iterator i = userList.iterator(); i.hasNext(); ) {
        User user_tmp = (User)i.next();
%>

<li><a href="administrator.do?action=user_info&id=<%=user_tmp.getId()%>"><%= user_tmp.getLastname() %>, <%= user_tmp.getFirstname() %></a></li>
<%  } %>
</ul>

<%@include file="WEB-INF/jspf/tags.jspf" %>
<%@include file="WEB-INF/jspf/footer.jspf" %>
